package utils;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Font;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;

import entity.Pokemon;

public class ResourceManager {
	private static String pokemonListFile = new String("data/pokemonsList.txt");
	private static PropertiesFile resources = new PropertiesFile("data/resources.cfg");
	private static Map<String, Image> images = new HashMap<String, Image>();
	private static Map<String, Font> fonts = new HashMap<String, Font>();
	private static Map<String, TiledMap> maps = new HashMap<String, TiledMap>();
//	private static ArrayList<Pokemon> pokemons = new ArrayList<Pokemon>();
	
	public static void init() {
		parseResourses();
//		createPokemons();
	}
	
	private static void parseResourses(){
		Set<Object> keys = resources.getAllProperties();
		for (Object k : keys) {
			String strKey = (String) k;
			try {
				System.out.println(strKey);
				createObject(strKey);				
			} catch (Exception ex) {
				// ex.printStackTrace();
				throw new RuntimeException("Can't create resource : " + strKey);
			}
		}
	}

	private static void createObject(String strKey) throws SlickException {
		String value = resources.getString(strKey);
		if (strKey.endsWith("Font")) {
			fonts.put(strKey, new AngelCodeFont(value + ".fnt", value + ".png",
					true));
		}else if(strKey.endsWith("Map")) {
			maps.put(strKey, new TiledMap(value));
		}else{
			images.put(strKey, new Image(value));
		}
	}

	
//	private static void createPokemons(){
//		try {
//			BufferedReader in = new BufferedReader(new FileReader(pokemonListFile));
//			
//			pokemons.add(new Pokemon(0));
//
//			try {
//				while (in.ready()) {
//					String str = in.readLine();
//					StringTokenizer st = new StringTokenizer(str, "$");
//					
//					Pokemon pokemon = new Pokemon();
//			
//					pokemon.setId(Integer.parseInt(st.nextToken()));
//					pokemon.setName(st.nextToken());
//					int hp = Integer.parseInt(st.nextToken());
//					pokemon.setCurrentHp(hp);
//					pokemon.setHp(hp);
//					pokemon.setAttack(Integer.parseInt(st.nextToken()));
//					pokemon.setDefence(Integer.parseInt(st.nextToken()));
//					pokemon.setSpeed(Integer.parseInt(st.nextToken()));
//					pokemon.setExpGain(Integer.parseInt(st.nextToken()));
//					pokemon.setType1(st.nextToken());
//					pokemon.setType2(st.nextToken());
//					pokemon.setAbilitie1(st.nextToken());
//					pokemon.setAbilitie2(st.nextToken());
//					pokemon.setRarity(Integer.parseInt(st.nextToken()));
//					pokemons.add(pokemon);					
//				}
//				System.out.println(pokemons.size());
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				// e.printStackTrace();
//				System.err.println("The file is not ready.");
//			}
//
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			// e.printStackTrace();
//			System.err.println("File not found.");
//		}
//	}
//	
	public static Image getImage(String name) {
		return images.get(name);
	}
	public static Font getFont(String fontName) {
		return fonts.get(fontName);
	}
	public static TiledMap getMap(String mapName) {
		return maps.get(mapName);
	}
//	public static Pokemon getPokemon(int id){		
//		Pokemon pokemon = new Pokemon();
//		for(int i = 0; i < pokemons.size(); i++){
//			if(pokemons.get(i).getId() == id){				
//				pokemon = pokemons.get(i);
//			}			
//		}
//		return pokemon;
//	}
//	public static Pokemon sortPokemonByRarity(int rarity){
//		Pokemon[] list = new Pokemon[150];
//		int j = 0;
//		for(int i = 0; i < pokemons.size(); i++){
//			if(pokemons.get(i).getRarity() > rarity){				
//				list[j++] = pokemons.get(i);
//			}			
//		}
//		int id =(int) Math.floor((Math.random() * j));
//		return list[id];
//	}
}
